import { Component, OnInit } from '@angular/core';
import {ActivatedRoute} from '@angular/router'
import { DomSanitizer } from '@angular/platform-browser';
import {ContractServiceSvr} from '../../services/contractServiceSvr'
import {PayRecordServiceSvr} from '../../services/PayRecordServiceSvr'
import { NzMessageService } from 'ng-zorro-antd/message';
@Component({
  selector: 'app-contract',
  templateUrl: './contract.component.html',
  styleUrls: ['./contract.component.less']
})
export class ContractComponent implements OnInit {

  // 是否显示加载
  isSpin:boolean=false
  // 合同文件docx和pdf
  file:string=''
  // 合同pdf
  PdfUrl:any=''
  // 合同id
  contractId:string=''
  // 押金
  rent:string=''
  // 用户id
  custmerID:string=''
  constructor(
    private route: ActivatedRoute,
    private sanitizer: DomSanitizer,
    private contractServiceSvr:ContractServiceSvr,
    private payRecordServiceSvr:PayRecordServiceSvr,
    private message: NzMessageService,
  ) { 
    this.route.queryParams.subscribe((r: any) => {
      this.file = r.valueOf().file;
      this.contractId = r.valueOf().contractId;
      this.rent = r.valueOf().rent;
      this.PdfUrl=this.file.split('|')
      this.custmerID=r.valueOf().custmerID;
    })
  }

  ngOnInit(): void {
  }

  // 确认签订合同
  isVisible = false;
  showModal(): void {
    this.payRecordServiceSvr.savePaymentRecord('',this.contractId,2,'',0).then((res)=>{
      console.log('savePaymentRecord:');
      console.log(res); 
      if(res==1){
       this.isVisible = true;
        setTimeout(() => {
          this.payRecordServiceSvr.getPayInfo('',this.custmerID,this.contractId).then((res)=>{
            console.log('getPayInfo:');
            this.payRecordId=res.Id
            this.actualMoney=res.PayablePrice
            console.log(res); 
            console.log('PayablePrice:'+res.PayablePrice);
            console.log('payRecordId:'+this.payRecordId); 
            console.log('actualMoney:'+this.actualMoney); 
          })
        }, 500);
      }
    }).catch((err)=>{
    this.isVisible = false;
      console.log('报错'+err.Msg);
      this.message.info(err.Msg);
    })
  }
// 缴费记录的Id
  payRecordId:string=''
// 实际缴费
actualMoney:any
frag:any

// 支付方式
payWay:number=2
  handleOk(): void {
    this.isSpin=true
    this.isVisible = false;
    console.log('actualMoney:'+this.actualMoney);
    this.payRecordServiceSvr.pay('',this.payRecordId,this.actualMoney,'',this.payWay).then((res)=>{
      console.log('pay');
      console.log(res);
      this.frag=res
      document.write(this.frag);
      location=location 
    }).catch((err)=>{
      this.isSpin=false
      console.log('报错'+err);
      this.message.info(err.Msg);
    })
  }

  handleCancel(): void {
    this.isVisible = false;
  }

}
